package com.bbm.communitysystem.controller.role;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Role;
import com.bbm.communitysystem.domain.bo.RoleBo;
import com.bbm.communitysystem.domain.dto.role.RoleDto;
import com.bbm.communitysystem.domain.vo.ManagePageRoleVo;
import com.bbm.communitysystem.domain.vo.PageVo;
import com.bbm.communitysystem.service.IRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色")
@Slf4j
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final IRoleService roleService;


    @Operation(summary = "新增角色")
    @PostMapping("/")
    public R addRole(@RequestBody RoleDto role) {
        return roleService.addRole(role) == 0 ? R.ok("新增成功") : R.error("新增失败");
    }

    @Operation(summary = "修改角色")
    @PutMapping("/")
    public R putRole(@RequestBody RoleDto role) {
        return roleService.putRole(role) == 0 ? R.ok("修改成功") : R.error("修改失败");
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    public R delRole(@PathVariable("id") Integer id) {
        return roleService.delRole(id) == 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Operation(summary = "获取角色")
    @GetMapping("/{id}")
    public R getRole(@PathVariable("id") Integer id) {

        return R.ok(roleService.getRole(id));
    }

    @Operation(summary = "获取角色列表")
    @GetMapping("/")
    public R getRoleList() {
        return R.ok(roleService.list());
    }


    @Operation(summary = "管理页面-获取角色列表")
    @GetMapping("/manage")
    public R<PageVo<ManagePageRoleVo>> getManageRoleList(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {

        Page<Role> rolePage = new Page<>(page, size);
        Page<Role> page1 = roleService.page(rolePage);


        PageVo<ManagePageRoleVo> pageVo = new PageVo<>(page1);

        page1.getRecords().forEach(item -> {
            RoleDto role = roleService.getRole(item);
            ManagePageRoleVo managePageRoleVo = BeanUtil.copyProperties(role, ManagePageRoleVo.class);
            managePageRoleVo.setRoleDescription(item.getRoleDescription());
            pageVo.getRecords().add(managePageRoleVo);
        });

        return R.ok("获取成功", pageVo);
    }


    @Operation(summary = "管理页面-获取角色选择列表")
    @GetMapping("/manage/list")
    public R<List<ManagePageRoleVo>> getSelectRoleList() {
        List<ManagePageRoleVo> list = roleService.list()
                .stream().map(item -> {
                    ManagePageRoleVo managePageRoleVo = BeanUtil.copyProperties(item, ManagePageRoleVo.class);
                    managePageRoleVo.setRoleDescription(item.getRoleDescription());
                    managePageRoleVo.setName(item.getRoleName());
                    managePageRoleVo.setId(item.getId());
                    return managePageRoleVo;
                }).toList();
        return R.ok("获取成功" ,list);
    }

    @Operation(summary = "管理页面-删除角色权限或路由")
    @PostMapping("/manage/del")
    public R delPer(@RequestParam("routeId") String routeId,
                    @RequestParam("perId") String perId) {

        RoleBo roleBo = new RoleBo();

        String[] split = routeId.split(",");
        Integer[] integers = new Integer[split.length];

        for (int i = 0; i < split.length; i++) {
            integers[i] = Integer.parseInt(split[i]);
        }

        String[] s = perId.split(",");
        Integer[] perIDs = new Integer[s.length];

        for (int i = 0; i < s.length; i++) {
            perIDs[i] = Integer.parseInt(s[i]);
        }

        roleBo.setPerId(perIDs);
        roleBo.setRouterId(integers);

        if (roleBo.getRouterId().length == 0) {
            return roleService.delPer(roleBo) == 0 ? R.ok("删除成功") : R.error("删除失败");
        }

        if (roleBo.getPerId().length == 0) {
            return roleService.delRouter(roleBo) == 0 ? R.ok("删除成功") : R.error("删除失败");
        }

        return roleService.delPerRouter(roleBo) == 0 ? R.ok("删除成功") : R.error("删除失败");
    }

}